import TRTCCalling from '@/utils/trtc-calling-js.js';
import tim from '@/utils/tim.js';
import store from '@/store';

let options = {
	SDKAppID: 1400577099, // 接入时需要将0替换为您的 SDKAppID
	// 从v0.10.2起，新增 tim 参数
	// tim 参数适用于业务中已存在 TIM 实例，为保证 TIM 实例唯一性
	tim: tim,
};
const trtcCalling = new TRTCCalling(options);

trtcCalling.setLogLevel(4);

//监听拨打语音回调
trtcCalling.on(TRTCCalling.EVENT.INVITED, async (payload) => {
	console.log('TRTCCalling.EVENT.INVITED 收到消息', payload);

	const { inviteID, sponsor, inviteData, isFromGroup } = payload;
	let { callType, roomID } = inviteData || {};

	let formUserInfo = await tim.getUserProfile({
		userIDList: [sponsor], // 请注意：即使只拉取一个用户的资料，也需要用数组类型，例如：userIDList: ['user1']
	});

	if (formUserInfo && formUserInfo.data && formUserInfo.data.length > 0) {
		let { avatar = '', nick = '' } = formUserInfo.data[0];

		let voiceVodioSessionContent = {
			formId: sponsor,
			formName: nick,
			formAvatar: avatar,
			formGroupId: '',
			callType: callType,
			callStatus: 0,
			inviteId: inviteID,
			roomId: roomID,
		};
		if (
			voiceVodioSessionContent.inviteId &&
			voiceVodioSessionContent.roomId &&
			voiceVodioSessionContent.callType
		) {
			store.commit(
				'imStore/SET_CURRENT_VOICEVIDEO_SESSION_CONTENT',
				voiceVodioSessionContent || null
			);
		}
	}

	// trtcCalling.accept({
	// 	inviteID: inviteID,
	// 	roomID: inviteData.roomID,
	// 	callType: inviteData.callType,
	// });
});

//监听拨打取消回调
trtcCalling.on(TRTCCalling.EVENT.CALLING_CANCEL, () => {
	//清空当前通话数据
	store.commit('imStore/CLEAR_CURRENT_VOICEVIDEO_SESSION_CONTENT');
});

//被邀请方 超时未答复
trtcCalling.on(TRTCCalling.EVENT.CALLING_TIMEOUT, () => {
	//清除SET_CURRENT_VOICEVIDEO_SESSION_CONTENT
	store.commit('imStore/CLEAR_CURRENT_VOICEVIDEO_SESSION_CONTENT');
});

trtcCalling.on(TRTCCalling.EVENT.CALLING_END, () => {
	console.log('挂断 trtcCalling。js');
	store.commit('imStore/CLEAR_CURRENT_VOICEVIDEO_SESSION_CONTENT');
});

export default trtcCalling;
